package com.mjsoftking.aopapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.mjsoftking.aopapp.Constants;
import com.mjsoftking.aopapp.R;
import com.mjsoftking.aopapp.aop.anno.CheckAuth;
import com.mjsoftking.aopapp.aop.anno.ClickHook;
import com.mjsoftking.aopapp.aop.anno.ClickHook2;
import com.mjsoftking.aopapp.aop.anno.ClickLimit;
import com.mjsoftking.aopapp.aop.anno.RequestLog;
import com.mjsoftking.aopapp.aop.aspect.AuthAspect;
import com.mjsoftking.aopapp.aop.aspect.ClickHookAspect;
import com.mjsoftking.aopapp.aop.aspect.ClickLimitAspect;
import com.mjsoftking.aopapp.aop.aspect.RequestLogAspect;
import com.mjsoftking.aopapp.event.NoLoginEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);

//        findViewById(R.id.text2).setOnClickListener(this);

        findViewById(R.id.text2).setOnClickListener(this);

        findViewById(R.id.text3).setOnClickListener(this);


    }


    @ClickHook
    @Override
    public void onClick(View v) {
        //todo ClickHook 必须放在具体activity类下，才能找到对应的 ClickHook2 标签方法
    }

//    /**
//     * 显示TOKEN信息，如果没有，则跳转登录页面
//     * {@link AuthAspect}
//     */
//    @CheckAuth
//    private void showToken() {
//        Toast.makeText(this, Constants.TOKEN, Toast.LENGTH_SHORT).show();
//    }

    /**
     * EventBus接收需要登录事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(NoLoginEvent event) {
        startActivity(new Intent(this, MainActivity2.class));
    }

    /**
     * 具体拦截查看 {@link RequestLogAspect}
     */
    @RequestLog
    public String test(String... args) {
        Log.e("TAG", "自身方法执行");
        return null != args ? args[0] : "";
    }

    /**
     * ClickHook2 具体拦截查看 {@link ClickHookAspect}
     * ClickLimit 具体拦截查看 {@link ClickLimitAspect}
     * CheckAuth 具体拦截查看 {@link AuthAspect}
     */
    @ClickHook2(R.id.text2)
    @ClickLimit
    @CheckAuth
    public void test2(View v) {
        Log.e("TAG", "ClickHook2, viewId = " + v.getId());
        Toast.makeText(this, Constants.TOKEN, Toast.LENGTH_SHORT).show();
    }

    /**
     * ClickHook2 具体拦截查看 {@link ClickHookAspect}
     * ClickLimit 具体拦截查看 {@link ClickLimitAspect}
     */
    @ClickHook2(R.id.text3)
    @ClickLimit
    public void test3(View v) {
        Log.e("TAG", "ClickHook2, viewId = " + v.getId());
        Constants.TOKEN = null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}